﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AMPLibrary;
using AMPLibrary.Media;
using AMPLibrary.LastFM;

namespace AMPLibrary.Controls
{
    /// <summary>
    /// Interaction logic for MusicMediaInformationControl.xaml
    /// </summary>
    public partial class MusicMediaInformationControl : UserControl
    {
        private MediaFile currentMedia;

        private LastFMArtistInformation lastFMArtistInformation;

        /// <summary>
        /// 
        /// </summary>
        public MusicMediaInformationControl()
        {
            InitializeComponent();

            Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            this.artistNameLabel.Content = string.Empty;
            this.titleLabel.Content = string.Empty;
            this.albumNameLabel.Content = string.Empty;
            this.descriptionTextBlock.Text = string.Empty;
            this.lastFMArtistInformation = new LastFMArtistInformation();

            this.toggleDescriptionToggleButton.Visibility = System.Windows.Visibility.Hidden;
            this.userMediaRating.Visibility = System.Windows.Visibility.Hidden;
            this.getMoreInfoToggleButton.Visibility = System.Windows.Visibility.Hidden;
            this.descriptionScrollViewer.Visibility = System.Windows.Visibility.Hidden;
            this.isFavoritedCheckBox.Visibility = System.Windows.Visibility.Hidden;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="media"></param>
        public void SetAudioMediaInformation(MediaFile media)
        {
            Clear();

            currentMedia = media;

            this.artistNameLabel.Content = media.Tag.AlbumArtist.ToLower();
            this.titleLabel.Content = media.Tag.Title.ToLower();
            this.albumNameLabel.Content = media.Tag.Album.ToLower();
            this.descriptionTextBlock.Text = string.Empty;

            this.userMediaRating.Visibility = System.Windows.Visibility.Visible;
            this.toggleDescriptionToggleButton.Visibility = System.Windows.Visibility.Visible;
            this.isFavoritedCheckBox.Visibility = System.Windows.Visibility.Visible;

            this.isFavoritedCheckBox.IsChecked = currentMedia.IsFavorited;

            this.userMediaRating.BindMedia(ref currentMedia);

            PopulateDescriptionChecked();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toggleDescriptionToggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (currentMedia == null)
                return;

            PopulateDescriptionChecked();
        }

        /// <summary>
        /// 
        /// </summary>
        private void PopulateDescriptionChecked()
        {
            Mouse.OverrideCursor = Cursors.Wait;

            if (toggleDescriptionToggleButton.IsChecked.Value)
            {
                // if it has information populate
                if (lastFMArtistInformation.HasInformation)
                {
                    Populate();
                    return;
                }
                else
                {
                    // if not try to get it then populate
                    lastFMArtistInformation.TryPopulateLastFMArtistInformation(currentMedia.Tag.AlbumArtist);

                    if (lastFMArtistInformation.HasInformation)
                        Populate();
                    else
                        this.descriptionTextBlock.Text = "Could not find artist information, or internet connection unavailable";
                }
            }
            else
            {
                this.descriptionTextBlock.Text = string.Empty;

                this.getMoreInfoToggleButton.Visibility = System.Windows.Visibility.Hidden;
                this.descriptionScrollViewer.Visibility = System.Windows.Visibility.Hidden;
            }

            Mouse.OverrideCursor = null;
        }

        /// <summary>
        /// 
        /// </summary>
        private void Populate()
        {
            this.descriptionTextBlock.Text = lastFMArtistInformation.BioLong;

            this.getMoreInfoToggleButton.Visibility = System.Windows.Visibility.Visible;
            this.descriptionScrollViewer.Visibility = System.Windows.Visibility.Visible;
        }

        private void getMoreInfoToggleButton_Click(object sender, RoutedEventArgs e)
        {
            //Mouse.OverrideCursor = Cursors.Wait;

            //MainWindow mainWindow = (MainWindow)App.Current.Windows[0];
            //ArtistInformationPage aip = ArtistInformationPage.GetArtistInformationPage();

            //if (mainWindow == null || aip == null)
            //    return;

            //mainWindow.SetPage(Pages.ArtistInformation);
            //aip.BindArtistInformation(lastFMArtistInformation);

            //Mouse.OverrideCursor = null;
        }

        private void isFavoritedCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            currentMedia.IsFavorited = isFavoritedCheckBox.IsChecked.Value;
        }
    }
}